﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RMS.Common;
using RMS.DB;

namespace RMS
{
    public partial class frmInsertCustomerInfo : ICommonForm
    {
        
        public frmInsertCustomerInfo()
        {
            InitializeComponent();
        }
        public string result
        {
            get
            {
                return String.Format("{0}#{1}#{2}#{3}#{4}#{5}#{6}", txtCusCode.Text.Trim(), txtName.Text.Trim(), txtAddress.Text.Trim(), txtPhone.Text.Trim(), txtEmail.Text.Trim(), Convert.ToString(rbIsVip.Checked), txtVipCode.Text.Trim());
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtCusCode.Text.Trim()))
            {
                MessageBox.Show("Hãy điền mã khách hàng.", "Chú ý");
                return;
            }
            if (string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                MessageBox.Show("Hãy điền tên khách hàng.", "Chú ý");
                return;
            }
            if (rbIsVip.Checked && string.IsNullOrEmpty(txtVipCode.Text.Trim()))
            {
                MessageBox.Show("Hãy điền mã thẻ VIP.", "Chú ý");
                return;
            }
            CustomersMng _customersMng = new CustomersMng();
            //if(!string.IsNullOrEmpty(txtVipCode.Text.Trim()))
            //{
            var cus = _customersMng.GetItemByVipCode(txtVipCode.Text.Trim());
            if (cus == null)
            {
                Customer item = _customersMng.GetItemByElements(txtCusCode.Text.Trim());
                if (item == null)
                    _customersMng.AddItem(txtCusCode.Text.Trim(), txtName.Text.Trim(), txtAddress.Text.Trim(), txtPhone.Text.Trim(),
                    txtEmail.Text.Trim(), 0, (rbIsVip.Checked) ? true : false, txtVipCode.Text.Trim());
            }
            //}


            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData(txtCusCode.Text.Trim());
        }
        private void LoadData(string id)
        {
            CustomersMng _customersMng = new CustomersMng();
            var item = _customersMng.GetItemByElements(id);
            if (item == null)
            {
                //MessageBox.Show("Không tìm thấy mã khách hàng này. Xin vui lòng kiểm tra lại", "Chú ý");
                rbNotVip.Checked = rbIsVip.Enabled = rbNotVip.Enabled = true;
                txtVipCode.Enabled = rbIsVip.Checked;
                txtAddress.Text =
                txtEmail.Text =
                txtName.Text =
                txtVipCode.Text =
                txtPhone.Text = "";
                return;
            }
            txtName.Text = item.Name;
            rbIsVip.Checked = Convert.ToBoolean(item.IsVip);
            rbNotVip.Checked = !Convert.ToBoolean(item.IsVip);
            if (rbIsVip.Checked)
            {
                txtVipCode.Text = Convert.ToString(item.VipCode);
                rbIsVip.Enabled = rbNotVip.Enabled = false;
                txtVipCode.Enabled = false;
            }
            //txtPhone.Text = item.PhoneNo;
            //txtEmail.Text = item.Email;
            //txtAddress.Text = item.Address;            
        }

        private void frmInsertCustomerInfo_Load(object sender, EventArgs e)
        {
            CustomersMng _customersMng = new CustomersMng();
            var autoCompleteSrc = _customersMng.GetAllItemIds();
            if (autoCompleteSrc.Count > 0)
            {
                var idCollection = new AutoCompleteStringCollection();
                idCollection.AddRange(autoCompleteSrc.ToArray());
                txtCusCode.AutoCompleteCustomSource = idCollection;
                txtCusCode.DataSource = autoCompleteSrc.ToArray();
            }
            txtCusCode.Text =
            txtAddress.Text =
            txtEmail.Text =
            txtName.Text =
            txtPhone.Text = "";
        }

        private void txtCusCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtCusCode.Text))
                return;
            btnSearch_Click(null, null);
        }

        private void rbIsVip_CheckedChanged(object sender, EventArgs e)
        {
            txtVipCode.Enabled = rbIsVip.Checked;
        }

        private void txtCusCode_Leave(object sender, EventArgs e)
        {
            
        }

        private void txtCusCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                btnSearch_Click(null, null);
            }
        }
    }
}
